package com.bjpowernode.controller;

import com.bjpowernode.model.TUser;
import com.bjpowernode.query.UserQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.UserService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户列表分页查询
     *
     * @param current
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:list')")
    @GetMapping(value = "/api/user")
    public R userPage(@RequestParam(value = "current", required = false) Integer current) {
        //required = false表示current参数可以不传；
        //required = true表示current参数必须要传，不传会报错；
        if (current == null) {
            current = 1;
        }

        PageInfo<TUser> pageInfo = userService.getUserByPage(current);

        return R.OK(pageInfo);
    }

    /**
     * 根据id查询用户详情
     *
     * @param id
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:view')")
    @GetMapping(value = "/api/user/{id}")
    public R userDetail(@PathVariable(value = "id") Integer id) {
        TUser tUser = userService.getUserById(id);

        return R.OK(tUser);
    }

    /**
     * 新增用户提交保存
     *
     * @param userQuery
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:add')")
    @PostMapping(value = "/api/user")
    public R addUser(UserQuery userQuery, @RequestHeader(value = "Authorization") String jwt) {
        //前端提交过来的是formData，后端怎么接收？
        //1、@RequestParam()注解一个一个字段接收
        //2、request.getParamter()原始的servlet方法一个一个字段接收
        //3、UserQuery 对象一次性接收，把参数封装到对象中了，一次性接收
        userQuery.setJwt(jwt);
        int save = userService.saveUser(userQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 编辑用户提交保存
     *
     * @param userQuery
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:edit')")
    @PutMapping(value = "/api/user")
    public R editUser(UserQuery userQuery, @RequestHeader(value = "Authorization") String jwt) {
        //前端提交过来的是formData，后端怎么接收？
        //1、@RequestParam()注解一个一个字段接收
        //2、request.getParamter()原始的servlet方法一个一个字段接收
        //3、UserQuery 对象一次性接收，把参数封装到对象中了，一次性接收
        userQuery.setJwt(jwt);
        int update = userService.updateUser(userQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:delete')")
    @DeleteMapping(value = "/api/user/{id}")
    public R delUser(@PathVariable(value = "id") Integer id) {
        int del = userService.delUserById(id);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 根据id批量删除用户
     *
     * @param ids
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:delete')")
    @DeleteMapping(value = "/api/user")
    public R batchDelUser(@RequestParam(value = "ids") String ids) {
        // ids = "1,3,5,6,11,13,16";
        List<String> idList = Arrays.asList(ids.split(","));

        int batchDel = userService.batchDelUserById(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }

    /**
     * 查询负责人（所有）
     *
     * @return
     */
    @GetMapping(value = "/api/user/owner")
    public R ownerUser() {
        List<TUser> ownerUserList = userService.getOwnerUser();
        return R.OK(ownerUserList);
    }
}
